1354D - Multiset - CodeForces Solution


binary search data structures *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define pii pair<int,int>
#define vi vector <int>

const int N = 1e6+9 , INF = 1e9+9 , mod=1e9+7 , B=450 ;
int  fn[N];
void ADD(int ind , int val){
    for(int i=ind ; i<N ; i+= i & -i)fn[i]+=val;
}
int GET(int ind){
    int sum=0;
    for(int i=ind ; i>0 ; i-= i & -i){
        sum+=fn[i];
    }
    return sum;
}
int bs(int x){
    int l=0 , r=N , mid=(l+r)/2;
    while(r-l>1){
        if(GET(mid)<=x)l=mid;
        else r=mid;
        mid=(l+r)/2;
    }
    return l;
}

int32_t main(){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    int n , m;
    cin  >> n >> m;
    int mx=0;
    while(n--){
        int a;
        cin >> a;
        mx=max(mx , a);
        ADD(a+1 , 1);
    }
    while(m--){
        int q;
        cin >> q;
        if(q>0){
            ADD(q+1 , 1);
        }
        else{
            q=-(q+1);
            ADD(bs(q)+1 , -1);
        }
    }
    int ans=bs(0);
    cout << (ans>1000000 ? 0 : ans) << endl;

}


Comments

Submit
0 Comments
More Questions

1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot